package com.zzyl.nursing.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.service.IWechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@Service
public class WechatServiceImpl implements IWechatService{
    /**
     * 获取openid
     *
     * @param code
     * @return
     */
    @Value("${wechat.appid}")
    private String appid;
    @Value("${wechat.secret}")
    private String secret;
    @Override
    public String getOpenid(String code) {
        String url = "https://api.weixin.qq.com/sns/jscode2session";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid",appid);
        paramMap.put("secret",secret);
        paramMap.put("js_code",code);
        paramMap.put("grant_type","authorization_code");
        String result = HttpRequest.get(url).form(paramMap).execute().body();
        System.out.println("result:"+result);
        JSONObject jsonObject = JSONUtil.parseObj(result);

        String openid = jsonObject.getStr("openid");
        return openid;
    }

    /**
     * 获取手机号
     *
     * @param detailCode
     * @return
     */
    @Override
    public String getPhone(String detailCode) {
        String accessToken = getAccessToken();
        String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="+accessToken;
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("code",detailCode);
        String result = HttpRequest.post(url).body(JSONUtil.toJsonStr(paramMap)).execute().body();
        System.out.println("result:"+ result);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        Integer errcode = jsonObject.getInt("errcode");
        if (errcode!=null && errcode!=0){
            throw new BaseException("获取手机号失败");
        }
        JSONObject phoneInfo = jsonObject.getJSONObject("phone_info");
        if (phoneInfo!=null){
            return phoneInfo.getStr("phoneNumber");
        }else{
            throw new BaseException("获取手机号失败");
        }
//        String phoneNumber = jsonObject.getJSONObject("phone_info").getStr("phoneNumber");
//        System.out.println("phoneNumber:"+ phoneNumber);
//        return phoneNumber;
    }

    /**
     * 获取access_token，这个东西是调用其他微信接口的一个凭据
     * 存活时间：7200s ，每日2000次
     * @return
     */
    public String getAccessToken(){
        String url = "https://api.weixin.qq.com/cgi-bin/token";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("grant_type","client_credential");
        paramMap.put("appid",appid);
        paramMap.put("secret",secret);
        String result = HttpRequest.get(url).form(paramMap).execute().body();
        JSONObject jsonObject = JSONUtil.parseObj(result);
        String accessToken = jsonObject.getStr("access_token");
        return accessToken;
    }
}
